package com.boarsoft.config.core;

import java.io.File;
import java.io.FileFilter;
import java.util.Map;

/**
 * 配置文件同步后执行动作
 * 
 * @author Mac_J
 *
 */
public class ConfigListenerImpl implements ConfigListener {
	/** 相对于当前应用实例运行目录的配置文件存储目录路径 */
	protected String absoluteDir = "";
	/** 相对于当前应用实例运行目录的配置文件存储目录路径 */
	protected String relativeDir = "";
	/** */
	protected Map<String, Configable> configableMap;
	/** */
	protected String regex = ".*";
	/** */
	protected FileFilter fileFilter = new FileFilter() {
		@Override
		public boolean accept(File f) {
			return f.getName().matches(regex);
		}
	};

	public ConfigListenerImpl() {
		absoluteDir = this.getClass().getClassLoader()//
				.getResource(".").getFile().concat(relativeDir);
	}

	@Override
	public boolean onReady(String frp) {
		if (configableMap.containsKey(frp)) {
			configableMap.get(frp).config();
		}
		return true;
	}

	@Override
	public boolean onRemove(String frp) {
		// Nothing to do
		return false;
	}

	@Override
	public String getAbosoluteDir() {
		return absoluteDir;
	}

	@Override
	public String getAbosultePath(String rp) {
		return new StringBuilder(absoluteDir)//
				.append(relativeDir).append(rp).toString();
	}

	@Override
	public String getAbosultePath() {
		return absoluteDir.concat(relativeDir);
	}

	@Override
	public String getRelativeDir() {
		return relativeDir;
	}

	public void setRelativeDir(String relativePath) {
		this.relativeDir = relativePath;
	}

	public Map<String, Configable> getConfigableMap() {
		return configableMap;
	}

	public void setConfigableMap(Map<String, Configable> configableMap) {
		this.configableMap = configableMap;
	}

	public String getRegex() {
		return regex;
	}

	public void setRegex(String regex) {
		this.regex = regex;
	}

	@Override
	public FileFilter getFileFilter() {
		return fileFilter;
	}

	public void setFileFilter(FileFilter fileFilter) {
		this.fileFilter = fileFilter;
	}
}